using System;
using System.Collections.Generic;
using UnityEngine;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CL
{
    public class CLFiber : IDisposable
    {
        internal static CLFiber Instance;

        public bool IsDisposed;

        public int Id;

        public CLScene Root { get; }

        public CLEntitySystem EntitySystem { get; }

        public ICLLog Log { get; }

        internal CLFiber(int id, int zone, string name)
        {
            this.Id = id;
            this.EntitySystem = new CLEntitySystem();
            this.Root = new CLScene(this, id, 1, name);
            this.Log = CLLogger.Instance.Log;
        }

        internal void Update()
        {
            try
            {
                this.EntitySystem.Update();
            }
            catch (Exception e)
                {
                    UnityEngine.Debug.LogError(e);
                }
        }

        internal void LateUpdate()
        {
            try
            {
                this.EntitySystem.LateUpdate();
   
            }
            catch (Exception e)
                {
                    UnityEngine.Debug.LogError(e);
                }
        }

        public void Dispose()
        {
            if (this.IsDisposed)
            {
                return;
            }
            this.IsDisposed = true;

            this.Root.Dispose();
        }
    }
}
